<template>
  <div class="ep-container">
    <el-form-item :label="options.label" :prop="options.name" :show-message="!isDetail" class="ep-form-item">
      <el-input
        v-if="!isDetail"
        class="ep-upperamount-container"
        v-model="fieldModel"
        type="text"
        :readonly="options.readonly"
        :placeholder="options.placeholder"
      />
      <div v-else class="ep-detail-content">
        {{ fieldModel }}
      </div>
    </el-form-item>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, watch } from 'vue'
import defaultProps from '../props'

const name = 'ep-upperamount'
Symbol(name)

export default defineComponent({
  name,
  props: defaultProps,
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const isDetail = computed(() => props.options.permission > 0)
    const fieldModel = computed({
      get() {
        if (isDetail.value && !props.modelValue) {
          return '--'
        }
        return props.modelValue as string
      },
      set(val: string) {
        emit('update:modelValue', val)
      }
    })

    let timeout: any
    watch(
      () => props.formData,
      (data) => {
        const { selectSumName } = props.options
        if (selectSumName) {
          clearTimeout(timeout)
          timeout = setTimeout(() => {
            if (data[selectSumName]) {
              const num = amountToChinese(data[selectSumName])
              emit('update:modelValue', num)
            } else {
              emit('update:modelValue', '')
            }
          }, 50)
        }
      },
      { deep: true }
    )

    /** 大写金额转换 */
    function amountToChinese(amount: any) {
      const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
      const cnIntRadice = ['', '拾', '佰', '仟']
      const cnIntUnits = ['', '万', '亿', '兆']
      const cnDecUnits = ['角', '分']
      const cnInteger = '整'
      const cnIntLast = '元'
      const maxNum = props.options.max || Number('999999999999999.99') // 最大金额限制
      let integerNum
      let decimalNum
      let chineseStr = ''
      let parts

      if (typeof amount !== 'string') {
        amount = amount.toString()
      }

      if (amount === '') {
        return ''
      }

      // 处理负数
      let isNegative = false
      if (amount.startsWith('-')) {
        isNegative = true
        amount = amount.slice(1)
      }

      // 拆分整数和小数部分
      if (amount.indexOf('.') === -1) {
        integerNum = amount
        decimalNum = ''
      } else {
        parts = amount.split('.')
        integerNum = parts[0]
        decimalNum = parts[1].substr(0, 2) // 只保留两位小数
      }

      if (integerNum > maxNum) {
        return ''
      }

      // 处理整数部分
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0
        const IntLen = integerNum.length
        for (let i = 0; i < IntLen; i++) {
          const n = integerNum.substr(i, 1)
          const p = IntLen - i - 1
          const q = Math.floor(p / 4)
          const m = p % 4
          if (n === '0') {
            zeroCount++
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0]
            }
            zeroCount = 0
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
          }
          if (m === 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q]
          }
        }
        chineseStr += cnIntLast
      }

      // 处理小数部分
      if (decimalNum !== '') {
        const decLen = decimalNum.length
        for (let i = 0; i < decLen; i++) {
          const n = decimalNum.substr(i, 1)
          if (n !== '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i]
          }
        }
      }

      // 处理零元整的情况
      if (chineseStr === '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger
      } else if (decimalNum === '') {
        chineseStr += cnInteger
      }

      // 处理负数
      if (isNegative) {
        chineseStr = '负' + chineseStr
      }

      return chineseStr
    }

    return {
      fieldModel,
      isDetail
    }
  }
})
</script>
